import cgi
from re import search
class url_request():

    def __init__(self,environ,cookie,sess,echo_to_page,header,code,post,sql,native_form,config):
        '''        
        Defining modules to work with them        
        '''
        self.environ=environ

        '''        
        Cookie Object
        '''
        self.cookie=cookie

        self.psql=sql

        self.config=config
        '''        
        Sessions
        '''
        self.session=sess

        '''
        Echo to page (testing)
        '''
        self.write=echo_to_page

        '''
        Post data (text only - not for files )        
        '''
        self.post=post

        self.set_status_code=code

        self.set_header=header

        self.form=native_form

        self.ajax=self.is_ajax()




    '''
    This is function for testing
    User return redirect
    '''

    def access(self,key):
        if self.session['permissions']:
            p=self.session['permissions']
            if p.has_key('superuser'):
                return True
            if p.has_key(key):
                return p[key]
            else:
                return False
        else:
            return False

    def redirect(self,url=None):
        self.set_header('Location',url if url else '/')


    def is_ajax(self):
        if 'HTTP_X_REQUESTED_WITH' in self.environ:
            return True
        else:
            return False

    def url_match(self,condition):
        searching=search(condition,self.environ['REQUEST_URI'])
        if searching:
            return searching.groups()
        else:
            return False










